<?php


namespace app\common\service\order\coupon_order\fake_order;


use api\channel\jutuike\Coupon;
use api\channel\jutuike\TravelUnion;
use api\channel\meituan\alliance\Seckill;
use app\common\service\order\coupon_order\jutuike_channel\JutuikeChannelBase;
use app\common\service\order\coupon_order\jutuike_channel\Union;
use app\model\CouponOrder;

class FakeOrder extends FakeOrderBase
{

    public $agent_id;
    public $serv_type;
    public $user_id;


    public function __construct($agent_id, $serv_type, $user_id)
    {

        $this->agent_id = $agent_id;
        $this->user_id = $user_id;
        $this->serv_type = $serv_type;
    }


    public function syncOrder()
    {
        $this->saveOrder();
    }

    public function getServType()
    {
        return $this->serv_type;
    }

    public function getProductName()
    {
        return  @PRODUCT_SERV_TYPE[$this->serv_type]['name'].'测试订单:'.$this->serv_type;
//        return '测试数据';
    }

    public function getAgentSettleStatus()
    {
        return CouponOrder::C_AGENT_SETTLE_STATUS_WAIT;
    }

    public function getStatus()
    {
        return CouponOrder::C_STATUS_RECHARGE_SUCCESS;
    }

    public function getProductImage()
    {
        return 'https://img.jutuike.com/taokeout/icon/kfc.png';
    }



    /**
     * 获取订单
     */
    public function httpGetOrder()
    {

        $query = [];

        for ($i = 1; $i < 9999999; $i++) {
            $query['page'] = $i;
            /**
             * @var TravelUnion
             * @var Coupon
             * @var Seckill
             */

            $orders = $this->extend->orders($query);

            if ($this->extend->supplierInfo->serv_type == 12) {

            }


            if (empty($orders)) break;

            if (empty($orders['data'])) break;



            foreach ($orders['data'] as $order) {
                /**
                 * @var JutuikeChannelBase $obj
                 * @var Union $obj
                 */
                $obj = new $this($this->agent_id, $this->serv_type, $this->user_id);

                try {

                    $obj->setExtendApi($this->extend)
                        ->setParams($order)
                        ->syncOrder();

                    var_dump($obj->id);exit;
                } catch (\Exception $exception) {
                    $msgs = [
                        'msg' => '拉取CPS 订单错误',
                        'exception' => $exception->getMessage(),
                        'file' => $exception->getFile(),
                        'line' => $exception->getLine(),
                        'httpData' => $order,
                        'supplier_name' => $this->extend->supplierInfo->name,
                        'supplier_id' => $this->extend->supplierInfo->id,
                    ];
                    \think\facade\Log::warning(json_encode($msgs));
                    continue;
                }


            }
            if (!empty($orders['last_page'])) {
                //判断是否已经是最后一页了
                if ($i >= $orders['last_page']) break;
            }

        }

        return $obj;
    }


}